import axios from "axios";
let baseURL = "http://192.168.0.114:8080/";
axios.interceptors.response.use(
  function(response) {
    // 对响应数据做点什么

    return response;
  },
  function(err) {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.reason = "请求错误(400)";
          break;
        case 401:
          err.reason = "未授权，请重新登录(401)";
          break;
        case 403:
          err.reason = "拒绝访问(403)";
          break;
        case 404:
          err.reason = "请求出错(404)";
          break;
        case 408:
          err.reason = "请求超时(408)";
        case 409:
          err.reason = "此名称已经被使用,不能重复";
          alert(err.reason);
          break;
        case 500:
          err.reason = "服务器错误(500)";
          break;
        case 501:
          err.reason = "服务未实现(501)";
          break;
        case 502:
          err.reason = "网络错误(502)";
          break;
        case 503:
          err.reason = "服务不可用(503)";
          break;
        case 504:
          err.reason = "网络超时(504)";
          break;
        case 505:
          err.reason = "HTTP版本不受支持(505)";
          break;
        default:
          err.reason = `连接出错(${err.response.status})!`;
      }
    } else {
      err.reason = "连接服务器失败!";
    }
    return Promise.reject(err);
  }
);
function isRepeatPath(path) {
  if (!path.startsWith(baseURL)) {
    return baseURL + path;
  } else {
    return path;
  }
}
function get(path) {
  path = isRepeatPath(path);
  return axios.get(path);
}
function getAndProperty(path) {
  path = isRepeatPath(path);
  return new Promise((resolve, reject) => {
    axios
      .get(path)
      .then(function(response) {
        if (response.status == "200") {
          resolve(response.data);
        }
      })
      .catch(function(error) {
        reject(error);
      });
  });
}
function getAndAssociate(path, property, isMerge) {
  getAndProperty(path).then(
    response => {
      let entry = response;
      let links = response._links[property];
      let href = links.href;
      console.log(href);
      axios
        .get(href)
        .then(function(response) {
          if (response.status == "200") {
            if (isMerge) {
              entry._links[property] = response.data._embedded[property];
              console.log(entry);
              entry;
            } else {
              console.log(entry);
              console.log(response.data);
              response.data;
            }
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    error => {
      console.log(error);
    }
  );
}
function save(path, payload) {
  path = isRepeatPath(path);
  return axios.post(path, payload);
}
function remove(path) {
  path = isRepeatPath(path);
  return axios.delete(path);
}
// 谁拥有谁,谁先写
function associate(path, playload) {
  path = isRepeatPath(path);
  let associateUrl = baseURL + playload;
  // 发送 PUT 请求
  return axios({
    method: "post",
    url: path,
    data: associateUrl,
    headers: { "Content-Type": "text/uri-list" }
  });
}
function associateByPut(path, playload) {
  path = isRepeatPath(path);
  let associateUrl = baseURL + playload;
  // 发送 PUT 请求
  return axios({
    method: "put",
    url: path,
    data: associateUrl,
    headers: { "Content-Type": "text/uri-list" }
  });
}
function update(path, payload) {
  path = isRepeatPath(path);
  return axios.patch(path, payload);
}
export {
  axios,
  baseURL,
  associate,
  get,
  getAndAssociate,
  getAndProperty,
  save,
  remove,
  update,
  associateByPut
};
